/*
 * Copyright 2005 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.allinclusive.tools.srcgen.database;

import java.sql.Types;

import com.googlecode.allinclusive.tools.srcgen.converter.JavaBeanUtils;

/**
 * This class holds information about an introspected column.  The
 * class has utility methods useful for generating iBATIS objects.
 * 
 */
public class ColumnDefinition {
    private String actualColumnName;
    
    private String description;
    
    private int jdbcType;

    private boolean nullable;

    private int length;

    private int scale;

    private String javaProperty;
    
    private String typeName;

    private boolean identity;
    
    private boolean isColumnNameDelimited;
    
    private JavaTypeResolver javaTypeResolver;

    public int getJdbcType() {
        return jdbcType;
    }

    public void setJdbcType(int jdbcType) {
        this.jdbcType = jdbcType;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

	/*
	 * This method is primarily used for debugging, so we don't externalize the strings
	 */
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append("Actual Column Name: "); //$NON-NLS-1$
        sb.append(actualColumnName);
        sb.append(", JDBC Type: "); //$NON-NLS-1$
        sb.append(jdbcType);
        sb.append(", Type Name: "); //$NON-NLS-1$
        sb.append(typeName);
        sb.append(", Nullable: "); //$NON-NLS-1$
        sb.append(nullable);
        sb.append(", Length: "); //$NON-NLS-1$
        sb.append(length);
        sb.append(", Scale: "); //$NON-NLS-1$
        sb.append(scale);
        sb.append(", Identity: "); //$NON-NLS-1$
        sb.append(identity);

        return sb.toString();
    }

    public void setActualColumnName(String actualColumnName) {
        this.actualColumnName = actualColumnName;
        isColumnNameDelimited = (actualColumnName != null && actualColumnName.indexOf(' ') != -1);
    }

    /**
     * @return Returns the identity.
     */
    public boolean isIdentity() {
        return identity;
    }

    /**
     * @param identity
     *            The identity to set.
     */
    public void setIdentity(boolean identity) {
        this.identity = identity;
    }

    public boolean isBLOBColumn() {
        return jdbcType == Types.BLOB
	        || jdbcType == Types.CLOB
	        || jdbcType == Types.LONGVARBINARY
	        || jdbcType == Types.LONGVARCHAR;
    }
    
    public boolean isJdbcCharacterColumn() {
        return jdbcType == Types.CHAR
            || jdbcType == Types.CLOB
            || jdbcType == Types.LONGVARCHAR
            || jdbcType == Types.VARCHAR;
    }

    public String getJavaProperty() {
        return getJavaProperty(null);
    }

    public String getJavaProperty(String prefix) {
        if (prefix == null) {
            return javaProperty;
        }
        
        StringBuffer sb = new StringBuffer();
        sb.append(prefix);
        sb.append(javaProperty);
        
        return sb.toString();
    }

    public void setJavaProperty(String javaProperty) {
        this.javaProperty = javaProperty;
    }

    
    public boolean isJDBCDateColumn() {
    	return jdbcType == Types.DATE;
    }
    
    public boolean isJDBCTimeColumn() {
    	return jdbcType == Types.TIME
    		|| jdbcType == Types.TIMESTAMP;
    }
    
    public String getIbatisFormattedParameterClause() {
        return getIbatisFormattedParameterClause(null);
    }
    
    public String getIbatisFormattedParameterClause(String prefix) {
        StringBuffer sb = new StringBuffer();
        
        sb.append('#');
        sb.append(getJavaProperty(prefix));
        sb.append('#');
        
        return sb.toString();
    }

    public String getActualColumnName() {
        return actualColumnName;
    }

    public void setColumnNameDelimited(boolean isColumnNameDelimited) {
        this.isColumnNameDelimited = isColumnNameDelimited;
    }

    public boolean isColumnNameDelimited() {
        return isColumnNameDelimited;
    }

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public JavaTypeResolver getJavaTypeResolver() {
		return javaTypeResolver;
	}

	public void setJavaTypeResolver(JavaTypeResolver javaTypeResolver) {
		this.javaTypeResolver = javaTypeResolver;
	}
	
	public String getGetterMothodName() {
		return JavaBeanUtils.getGetterMothodName(javaProperty);
	}
	public String getSetterMothodName() {
		return JavaBeanUtils.getSetterMothodName(javaProperty);
	}
	
}
